package com.fc.test.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fc.test.mapper.auto.EntInfoMapper;
import com.fc.test.mapper.auto.EntJobMapper;
import com.fc.test.model.auto.*;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.util.StrUtil;
import com.fc.test.common.base.BaseService;
import com.fc.test.common.support.ConvertUtil;
import com.fc.test.mapper.auto.StuCollectMapper;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.util.SnowflakeIdWorker;
import com.fc.test.util.StringUtils;

/**
 * 学生收藏表 StuCollectService
 * @Title: StuCollectService.java 
 * @Package com.fc.test.service 
 * @author DrRic_自动生成
 * @email drhyz@qq.com
 * @date 2020-07-05 17:21:58  
 **/
@Service
public class StuCollectService implements BaseService<StuCollect, StuCollectExample>{
	@Autowired
	private StuCollectMapper stuCollectMapper;
	@Autowired
	EntJobMapper entJobMapper;
	@Autowired
	EntInfoMapper entInfoMapper;
	
      	   	      	      	      	      	      	
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<Object> list(Tablepar tablepar,StuCollect record){
	        StuCollectExample testExample=new StuCollectExample();
	        if(record.getSid()!=null){
				testExample.createCriteria().andSidEqualTo(record.getSid());
				testExample.setOrderByClause("scid ASC");
				if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
					testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
				}
				PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
				JSONArray result = new JSONArray();
				List<StuCollect> list= stuCollectMapper.selectByExample(testExample);
				for(StuCollect stuCollect:list){
					JSONObject obj=new JSONObject();

					obj.put("eid",stuCollect.getEid());
					EntInfo entInfo=entInfoMapper.selectByPrimaryKey(stuCollect.getEid());
					EntJob entJob=entJobMapper.selectByPrimaryKey(stuCollect.getJid());
					obj.put("ename",entInfo!=null?entInfo.getEname():"");
					obj.put("jobname",entJob!=null?entJob.getName():"");

					obj.put("jid",stuCollect.getJid());

					result.add(obj);

				}

				PageInfo<Object> pageInfo = new PageInfo<Object>(result);
				return  pageInfo;
			}
	        else{
	        	return new PageInfo<Object>();
			}
	 }

	public PageInfo<StuCollect> listForSys(Tablepar tablepar, StuCollect record){
		StuCollectExample testExample=new StuCollectExample();
		testExample.createCriteria().andLikeQuery(record);
		testExample.setOrderByClause("scid ASC");
		if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
			testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
		}
		PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
		List<StuCollect> list= stuCollectMapper.selectByExample(testExample);
		PageInfo<StuCollect> pageInfo = new PageInfo<StuCollect>(list);
		return  pageInfo;
	}

	@Override
	public int deleteByPrimaryKey(String ids) {
					
			Integer[] integers = ConvertUtil.toIntArray(",", ids);
			List<Integer> stringB = Arrays.asList(integers);
			StuCollectExample example=new StuCollectExample();
			example.createCriteria().andScidIn(stringB);
			return stuCollectMapper.deleteByExample(example);
			
				
	}
	
	
	@Override
	public StuCollect selectByPrimaryKey(String id) {
				
			Integer id1 = Integer.valueOf(id);
			return stuCollectMapper.selectByPrimaryKey(id1);
				
	}

	
	@Override
	public int updateByPrimaryKeySelective(StuCollect record) {
		return stuCollectMapper.updateByPrimaryKeySelective(record);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(StuCollect record) {
				
		record.setScid(null);
		
				
		return stuCollectMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(StuCollect record, StuCollectExample example) {
		
		return stuCollectMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(StuCollect record, StuCollectExample example) {
		
		return stuCollectMapper.updateByExample(record, example);
	}

	@Override
	public List<StuCollect> selectByExample(StuCollectExample example) {
		
		return stuCollectMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(StuCollectExample example) {
		
		return stuCollectMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(StuCollectExample example) {
		
		return stuCollectMapper.deleteByExample(example);
	}
	
	/**
	 * 检查name
	 * @param stuCollect
	 * @return
	 */
	public int checkNameUnique(StuCollect stuCollect){
		StuCollectExample example=new StuCollectExample();
		example.createCriteria().andJidEqualTo(stuCollect.getJid());
		List<StuCollect> list=stuCollectMapper.selectByExample(example);
		return list.size();
	}


}
